﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ASoft.WPF
{
    public class HttpPostService
    {
        static DateTime nextTime = DateTime.Now;
        static List<PostInfo> postInfos = new List<PostInfo>();
        static object lockKey = new object();

        string HttpPost(string uri, string postData)
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
            //req.Proxy = new System.Net.WebProxy(ProxyString, true);
            //Add these, as we're doing a POST
            req.ContentType = "application/x-www-form-urlencoded";
            //req.ContentType = "application/x-www-form";
            req.Method = "POST";
            //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postData);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length); //Push it out there
            os.Close();
            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return null;
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }

        public void AddPost(string uri, string postData)
        {
            lock (lockKey)
            {
                postInfos.Add(new PostInfo(uri, postData));
            }

        }

        public void Post()
        {
            if (postInfos.Count == 0)
            {
                return;
            }
            List<PostInfo> list;
            lock (lockKey)
            {
                list = postInfos.ToList();
                postInfos.Clear();
            }
            list.ForEach(p =>
                {
                    PrivateHttpPost(p.Uri, p.PostData);
                });
        }

        private void PrivateHttpPost(string uri, string postData)
        {
            if (DateTime.Now < nextTime)
            {
                return;
            }
            var result = "";
            try
            {
                result = HttpPost(uri, postData);
            }
            catch (System.Net.WebException ex)
            {
                var excep = ex.InnerException as System.Net.Sockets.SocketException;
                if (excep != null && excep.ErrorCode == 10061)
                {
                    nextTime = DateTime.Now.AddMinutes(1);
                }
                RC.Current.Loger.Error(ex.ToString());
            }
            catch (Exception e)
            {
                RC.Current.Loger.Error(e.ToString());
            }
        }

        public class PostInfo
        {
            public PostInfo(string uri, string postData)
            {
                this.Uri = uri;
                this.PostData = postData;
            }

            public string Uri { get; private set; }
            public string PostData { get; private set; }
        }
    }
}
